package com.viknix.threadpool.manager.client.core;

import com.viknix.threadpool.manager.client.builder.ThreadPoolBuilder;
import com.viknix.threadpool.manager.client.enums.BlockingQueueEnum;
import lombok.Data;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * @Author: Dongqi
 * @Date: 2021/10/21 15:48
 * @Version 1.0
 * @Description: 动态线程池包装类
 */
@Data
public class DynamicThreadPoolWrap {

    /**
     * 线程池id
     */
    private String threadPoolId;
    /**
     * 是否需要订阅远端线程池配置
     */
    private boolean subscribeFlag;
    /**
     * 动态线程池
     */
    private DynamicThreadPoolExecutor threadPool;

    /**
     * 如果没有传入线程池参数，那么会构建一个默认线程池对象填充 threadPool 属性
     *
     * @param threadPoolId 线程池id
     */
    public DynamicThreadPoolWrap(String threadPoolId) {
        this(threadPoolId, (DynamicThreadPoolExecutor) ThreadPoolBuilder.builder()
                .setThreadPoolId(threadPoolId)
                .setBlockingQueueCode(BlockingQueueEnum.LINKED_BLOCKING_QUEUE.getCode())
                .setDynamicThreadPool(true)
                .build());
    }

    public DynamicThreadPoolWrap(String threadPoolId, DynamicThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolId = threadPoolId;
        this.threadPool = threadPoolExecutor;
    }

    public void execute(Runnable command) {
        threadPool.execute(command);
    }

    public void shutdown() {
        threadPool.shutdown();
    }

    public List<Runnable> shutdownNow() {
        return threadPool.shutdownNow();
    }

    public boolean isShutdown() {
        return threadPool.isShutdown();
    }

    public Future<?> submit(Runnable task) {
        return threadPool.submit(task);
    }

    public <T> Future<T> submit(Runnable task, T result) {
        return threadPool.submit(task, result);
    }

    public <T> Future<T> submit(Callable<T> task) {
        return threadPool.submit(task);
    }
}
